package com.hotel.utils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期工具类
 */
public class DateUtil {
    /**
     * 时间格式
     */
    public static final String PATTEN_YMDHMS = "yyyy/MM/dd HH:mm:ss";
    public static final String PATTEN_YMD = "yyyy/MM/dd";
    public static final String PATTEN_HMS = "HH:mm:ss";

    /**
     * 字符串转换
     * @param str
     * @param parten
     * @return
     */
    public static Object stringToLocalDateTime(String str,String parten) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(parten);
        switch (parten){
            case PATTEN_YMDHMS:  return LocalDateTime.parse(str,formatter);
            case PATTEN_YMD:  return LocalDate.parse(str,formatter);
            case PATTEN_HMS:  return LocalTime.parse(str,formatter);
            default:   return null;
        }

    }

    /**
     * 时间对象转成字符串
     * @param localDateTime
     * @return
     */
    public static String timeToString(Object localDateTime) {
        DateTimeFormatter formatter;
        if (localDateTime instanceof LocalDate) {
            formatter = DateTimeFormatter.ofPattern(PATTEN_YMD);
            return ((LocalDate) localDateTime).format(formatter);
        }
        if (localDateTime instanceof LocalTime) {
            formatter = DateTimeFormatter.ofPattern(PATTEN_HMS);
            return ((LocalTime) localDateTime).format(formatter);
        }
        if (localDateTime instanceof LocalDateTime) {
            formatter = DateTimeFormatter.ofPattern(PATTEN_YMDHMS);
            return ((LocalDateTime) localDateTime).format(formatter);
        }
        return "无匹配对象";
    }
}
